﻿using System.ComponentModel;

namespace FlashCart.Core
{
    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBase"/> class.
        /// </summary>
        protected ViewModelBase()
        {
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            // NOTE:
            // The good way is use Expression to get Name of property, so when you call "OnPropertyChanged",
            // you no need to put "name" of property, only put Expression for prevent risk. Ex:
            // OnPropertyChanged("IsTrendEnable"); => OnPropertyChanged(() => IsTrendEnable);
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}